package com.xthink.test;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xthink.common.utils.StringUtils;
import com.xthink.common.utils.TranSidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public class test {
    private final static ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public static void main(String[] args) {

        HttpRequest request = HttpRequest.get("https://api.iot.10086.cn/v5/ec/get/token?appid=C5010100070967100100000&password=H4XUbBqpW,AF&transid=" + TranSidUtils.getTranSid());
        HttpResponse response = request.execute();
        String body = response.body();
        try {
            JsonNode jsonNode = mapper.readTree(body);
            if( ObjectUtils.isNull(jsonNode.get("result"))){
                // 这里就是没有token  就可以输出那边给的响应码和错误信息
                Integer status = jsonNode.get("status").asInt();
                System.out.println(status);
                String message = jsonNode.get("message").asText();
                System.out.println(message);
            }else {
                String token = jsonNode.get("result").get(0).get("token").asText();
                System.out.println(token);
//                redisTemplate.opsForValue().set("token",token,50, TimeUnit.SECONDS);
            }
        } catch (JsonProcessingException e) {
//            log.error(String.format("获取结果失败：%s",e.getMessage()));
            e.printStackTrace();
        }
//        String APPID ="C5010100070967100100000";
//
//        DateTime date = DateUtil.date();
//        String s1 = date.toString();
//        String yyyyMMddHHmmss = DateUtil.format(date, "yyyyMMddHHmmss");
//
//        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
//        long l = snowflake.nextId();
//        String s = Long.toString(l);
//        String substring = s.substring(s.length() - 8);
//
//        String tranSid = APPID + yyyyMMddHHmmss + substring;
//        System.out.println(tranSid);


    }
}
